-------------------------------------------------------------------------------
-- Functions needed to support the Conversion
-------------------------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetNumericDatabaseVersion]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetNumericDatabaseVersion]
GO

CREATE FUNCTION [dbo].[GetNumericDatabaseVersion]()
RETURNS int
AS
BEGIN
	declare @version VARCHAR(30)
	declare @numericVersion int

	select @version=VersionName
	from Version
	where Description = 'Insight'

	select @numericVersion = convert( int , 
	case when substring( @version , 1 , 1 ) LIKE '[0-9]' then substring( @version , 1 , 1 ) else '' end +
	case when substring( @version , 2 , 1 ) LIKE '[0-9]' then substring( @version , 2 , 1 ) else '' end +
	case when substring( @version , 3 , 1 ) LIKE '[0-9]' then substring( @version , 3 , 1 ) else '' end +
	case when substring( @version , 4 , 1 ) LIKE '[0-9]' then substring( @version , 4 , 1 ) else '' end +
	case when substring( @version , 5 , 1 ) LIKE '[0-9]' then substring( @version , 5 , 1 ) else '' end +
	case when substring( @version , 6 , 1 ) LIKE '[0-9]' then substring( @version , 6 , 1 ) else '' end +
	case when substring( @version , 7 , 1 ) LIKE '[0-9]' then substring( @version , 7 , 1 ) else '' end +
	case when substring( @version , 8 , 1 ) LIKE '[0-9]' then substring( @version , 8 , 1 ) else '' end +
	case when substring( @version , 9 , 1 ) LIKE '[0-9]' then substring( @version , 9 , 1 ) else '' end
	)

	return  @numericVersion
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FormatDatabaseVersion]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FormatDatabaseVersion]
GO

CREATE FUNCTION [dbo].[FormatDatabaseVersion]
(
	@numericVersion int
)
RETURNS VARCHAR(30)
AS
BEGIN

	declare @version varchar(30)
    set @version = cast(@numericVersion as varchar)
    set @version =
		SUBSTRING(@version, 1, 1) + '.' +
		SUBSTRING(@version, 2, 1) + '.' +
		SUBSTRING(@version, 3, 1) + '.' +
		SUBSTRING(@version, 4, 3)

	return @version
END
GO

-------------------------------------------------------------------------------
-- Setup the version for 2.3.1 release
-------------------------------------------------------------------------------
BEGIN

	IF ( [dbo].[GetNumericDatabaseVersion]() < 231000 )
	BEGIN

	declare @dbversion	VARCHAR(30)
	set @dbversion = null

		BEGIN
			set @dbversion = '2.3.1.000'
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END -- End of version setup

END
GO

-------------------------------------------------------------------------------
-- From here on, all the new changes will be added for 2.3.1 release
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.001
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231001 )
	BEGIN


		-- CJR20 TrackIt 8726.  Fix some incorrect protocol statuses in Insight (anything Active and Inactive, Lapsed)
		BEGIN
			update protocol set substatusid = null 
			where moduletype = 'A' and (overallstatusid = 1 or overallstatusid = 2 and substatusid = 8)
		END		

		BEGIN
			declare @dbversion varchar(30)
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.001

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.002
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231002 )
	BEGIN


		-- ys985 TrackIt 9990.  Fix IR application on the protocol details page is displaying the IR and a subsequent AME in the same application    
		BEGIN
			DECLARE @ppid INT
			DECLARE @protocolVersionId INT

			SELECT @ppid = ID 
			FROM PROTOCOLPROCESS 
			WHERE PROTOCOLID =(SELECT ID FROM PROTOCOL WHERE PROTOCOLNUMBER = '2009P000594') 
			AND NAME = 'IR'

			SELECT @protocolVersionId = Id FROM PROTOCOLVERSION WHERE PROTOCOLPROCESSID = @ppid

			Select QA.Id,  
			  QA.Name 
			INTO #temp_trk9990
 			FROM dbo.QuestionnaireAnswer QA  
  			INNER JOIN dbo.QuestionnaireVersion QV   
   				on QA.QuestionnaireVersionId = QV.ID  
  			INNER JOIN dbo.Questionnaire Q  
   				on QV.QuestionnaireId = Q.Id  
  			INNER JOIN dbo.ProtocolVersion_QuestionnaireAnswer_Map map  
   				on (map.QuestionnaireAnswerId = QA.Id and map.ProtocolVersionId = @protocolVersionId)  
  			LEFT JOIN dbo.RefCodeAttribute RCA ON RCA.RefCode = Q.Category AND RCA.Attribute = 'ORDER'
  			LEFT JOIN dbo.AdmPerson ADP
   				on ADP.Id = QA.LastModifiedByAdmPersonId
   			order by RCA.Value, Q.CategorySequenceNo
			-- select * from #temp_trk9990

			DELETE FROM PROTOCOLVERSION_QUESTIONNAIREANSWER_MAP WHERE protocolversionid = @protocolVersionId AND QuestionnaireAnswerId IN (SELECT ID FROM #temp_trk9990 WHERE NAME = 'Amendment')

			DROP TABLE #temp_trk9990
		END		

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.002

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.003
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231003 )
	BEGIN

		-- CJR20 TrackIt 9597
		BEGIN
			ALTER TABLE EmailTemplate Add [To] VARCHAR(100) NULL, [From] VARCHAR(100) NULL
			exec('INSERT INTO EmailTemplate ([Type], BodyText, ModuleType, Subject, [To], [From])
			VALUES (''ANIMALS_SAVECERTFAILEDEMAIL'', ''Rex Recom training tab was not successfully updated with the Occupational Health Survey and Qualifications and Experience information.  Please make the updates manually.    <br />    <br />Failed Username: {4}<br />Protocol Number: {0}<br />Protocol Title: {1}<br />PI Name: {2}<br />Process:  {3}<br /><br />'',
				''Animals'', ''Failure to update Recom with training info'', ''srac@partners.org'', ''srac@partners.org'')');
			UPDATE EmailTemplate SET ModuleType = 'Humans' WHERE ModuleType = 'EIRB'
		END

		-- CJR20 TrackIt 9812
		BEGIN
			DELETE script_questionnaireversion_map FROM
			script_questionnaireversion_map map 
			INNER JOIN questionnaireversion qv ON qv.id = map.questionnaireversionid
			INNER JOIN questionnaire q ON q.id = qv.questionnaireid
			WHERE q.name = 'Detailed Research Plan'

			DELETE FROM script WHERE scripttext LIKE '''Detail Research Plan%'
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.003

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.004
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231004 )
	BEGIN

		-- CJR20 TrackIt 10009
		BEGIN
			UPDATE TrainingSubType SET Name = 'CITI GCP', Description = 'CITI GCP' 
			WHERE Name = 'GCP'
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.004

-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- 2.3.1.005
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231005 )
--	BEGIN

		-- YS985 TrackIt 9554
		-- add user: Yung Soo Kim (YSK)
--		INSERT INTO [dbo].[Organization_WorkflowRole_Person_Map]
--           ([WorkflowRole]
--           ,[PersonId]
--           ,[OrganizationId]
--           )
--		VALUES
--           ('MGHInvestigationalDrugServiceContact'
--			,45053		
--			,3)
--
--		-- add user: Cheryl Reilly-Tremblay (CRT0) 
--		INSERT INTO [dbo].[Organization_WorkflowRole_Person_Map]
--           ([WorkflowRole]
--           ,[PersonId]
--           ,[OrganizationId]
--           )
--		VALUES
--           ('MGHInvestigationalDrugServiceContact'
--			,173015
--			,3)
--	END

	-- update dbversion after current trans
	BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
	END
END -- End 2.3.1.005
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- 2.3.1.006
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231006 )
	BEGIN
		-- IDK - TrackIt #9736 
		BEGIN
					
			update pp
			set  pp.Name = ps.FormsID
			from Protocolprocess pp
				join protocolsubmission ps
				on pp.ProtocolId = ps.ProtocolId
				and pp.id = ps.ProtocolProcessId
			where (pp.name like 'TR%' or pp.name like 'AR%')
			and pp.Name != ps.FormsID
			
		END

		
		-- CJR20 TrackIt 10239
		BEGIN
			insert into refcode (refdomain, refcode, refmeaning, active) 
			values ('SCRIPTTYPE', 'SCRIPTTYPE_TRACKSTAFFCHANGES', 'Track Staff Changes', 'Y')
		END
		
		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.006

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.007
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231007 )
	BEGIN

		-- CJR20 TrackIt 10122
		BEGIN
			insert into refcode (refdomain, refcode, refmeaning, active) 
			values ('PPSTATUS', 'PPSTATUS_WITHDRAWN', 'Withdrawn', 'Y')
		END
		
		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.007

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.008
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231008 )
	BEGIN

		-- VS949 TrackIt 9457 --Commented the code due to logic changes
--		BEGIN
--			--delete  from  PurPOAPSummary_Fct where fundnumber='103620' and OrderNumber='0002822358'
--		END
		
		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.008

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.009
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231009 )
	BEGIN

		-- CJR20 TrackIt 9812
		BEGIN
			Update RefCode SET RefCode = 'BHX' WHERE RefCode = 'BXH' and RefDomain = 'ANIMBUILDING'
			update answer set value = 'BHX' where value = 'BXH'
			Update QuestionnaireItem set parentvalue = 'BHX' WHERE parentvalue = 'BXH'

			DELETE Questionnaire_PrintOrder_Map WHERE PrintTypeKey = 'ANIMALPPTYPE_IR'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 1 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Abstract'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 2 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Standard Application'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 3 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Potential Pain and Distress'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 4 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Alternatives'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 5 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Refine'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 6 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Detailed Research Plan'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 7 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Duration, Clinical Signs, Endpoints and Euthanasia'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 8 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Housing'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 9 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Breeding Studies'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 10 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Controlled Food or Fluid Intake'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 11 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Anesthesia Regimen'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 12 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 13 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Multiple Survival Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 14 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Antibody & Ascites Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 15 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Tumor Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 16 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Morbidity and Mortality'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 17 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Pain and/or Distress Study Justification'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 18 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Paralytics'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 19 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Restraint'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 20 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Transportation'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 21 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'NHP Psychological Enrichment'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 22 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Dog Exercise'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 23 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Hazardous Agent Administration and Use'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 24 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Sponsor Funding'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 25 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Qualifications and Experience'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_IR', 26 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Initial Survey'

			DELETE Questionnaire_PrintOrder_Map WHERE PrintTypeKey = 'ANIMALPPTYPE_AM'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 1 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'IACUCAmendment'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 2 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Abstract'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 3 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Potential Pain and Distress'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 4 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Alternatives'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 5 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Refine'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 6 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Detailed Research Plan'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 7 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Duration, Clinical Signs, Endpoints and Euthanasia'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 8 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Housing'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 9 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Breeding Studies'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 10 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Controlled Food or Fluid Intake'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 11 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Anesthesia Regimen'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 12 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 13 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Multiple Survival Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 14 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Antibody & Ascites Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 15 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Tumor Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 16 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Morbidity and Mortality'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 17 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Pain and/or Distress Study Justification'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 18 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Paralytics'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 19 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Restraint'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 20 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Transportation'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 21 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'NHP Psychological Enrichment'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 22 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Dog Exercise'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 23 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Hazardous Agent Administration and Use'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 24 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Sponsor Funding'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_AM', 25 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Qualifications and Experience'

			DELETE Questionnaire_PrintOrder_Map WHERE PrintTypeKey = 'ANIMALPPTYPE_TR'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 1 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Abstract'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 2 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Triennial Review'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 3 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Potential Pain and Distress'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 4 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Alternatives'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 5 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Literature Search- Refine'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 6 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Detailed Research Plan'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 7 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Duration, Clinical Signs, Endpoints and Euthanasia'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 8 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Housing'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 9 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Breeding Studies'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 10 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Controlled Food or Fluid Intake'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 11 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Anesthesia Regimen'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 12 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 13 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Multiple Survival Procedures'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 14 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Antibody & Ascites Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 15 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Tumor Production'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 16 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Morbidity and Mortality'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 17 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Pain and/or Distress Study Justification'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 18 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Paralytics'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 19 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Restraint'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 20 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Transportation'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 21 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'NHP Psychological Enrichment'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 22 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Dog Exercise'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 23 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Hazardous Agent Administration and Use'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 24 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Sponsor Funding'
			INSERT INTO Questionnaire_PrintOrder_Map (questionnaireId, PrintTypeKey, PrintOrder)
			SELECT q.id, 'ANIMALPPTYPE_TR', 25 FROM Questionnaire q WHERE q.moduletype = 512 and q.name = 'Qualifications and Experience'
		END
		
		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.009

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.010
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231010 )
	BEGIN

		-- CJR20 TrackIt 10009
		BEGIN
			UPDATE TrainingSubType SET Name = 'GCP', Description = 'GCP' 
			WHERE Name = 'CITI GCP'
		END
		
		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.010

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 2.3.1.011
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231011 )
	BEGIN
		--VRG4 Trackitt 10056
		BEGIN
			--Disclosure Lookup
			DECLARE @ModuleRoleTypeId INT
			SELECT @ModuleRoleTypeId = Id FROM ModuleRoleType WHERE Name = 'Disclosure Lookup'
			--If Row not exists			
			IF(@ModuleRoleTypeId IS NULL)
			BEGIN
				--Inserting the Role
				INSERT INTO ModuleRoleType
				(Name,Description,BitValue,ModuleBitmask,IsGrantedViaUI)
				Values
				('Disclosure Lookup','Allows a user to search by person or company and view limited COI information',536870912,128,1)
			END
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.011

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 2.3.1.012
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231012 )
	BEGIN
		--KDH3 Arra project (Adding a role to set the IsFinalReport)
		BEGIN
			IF NOT EXISTS(SELECT 1 FROM [dbo].[ArraPIQuestionnaireEvent] WHERE id = 5) BEGIN
				SET IDENTITY_INSERT [dbo].[ArraPIQuestionnaireEvent] ON
				INSERT [dbo].[ArraPIQuestionnaireEvent] ([ID], [Name], [ArraPIQuestionnaireEventTypeID]) 
				VALUES (5, N'Follow-Up Notification', 1)
				SET IDENTITY_INSERT [dbo].[ArraPIQuestionnaireEvent] OFF
			END
			--Disclosure Lookup
			Set @ModuleRoleTypeId = null
			SELECT @ModuleRoleTypeId = Id FROM ModuleRoleType WHERE Name = 'Set Arra IsFinalReport'
			--If Row not exists			
			IF(@ModuleRoleTypeId IS NULL)
			BEGIN
				--Inserting the Role
				INSERT INTO ModuleRoleType
				(Name,Description,BitValue,ModuleBitmask,IsGrantedViaUI)
				Values
				('Set Arra IsFinalReport','Allows a user to set the ARRA IsFinalReport flag',1073741824,4,1)
			END
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.012

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.013
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231013 )
	BEGIN
		--ys985 Trackitt 10106
		BEGIN
			delete Organization_WorkflowRole_Person_Map where WorkflowRole = 'HROAdminPerson' and personid = 6070

		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.013

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.014
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231014 )
	BEGIN
		--kdh3 ARRA  (Storing CFDA number in ArraAgreement)
		BEGIN
			if not exists(select 1 from sys.columns c 
						  inner join sys.objects o on o.object_id = c.object_id and o.type = 'U'
						  where c.name = 'CFDANumber' and o.name = 'ArraAgreement')
			alter table dbo.ArraAgreement add CFDANumber varchar(6) null;
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.014

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 2.3.1.015
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231015 )
	BEGIN
		--kdh3 ARRA  (Column updates and adding table for GL close Preview Data)
		BEGIN
			if not exists(select 1 from sys.columns c 
						  inner join sys.objects o on o.object_id = c.object_id and o.type = 'U'
						  where c.name = 'CFDANumber' and o.name = 'ArraAgreement')
			alter table dbo.ArraAgreement add CFDANumber varchar(6) null;
			if exists(select 1 from sys.columns c 
						  inner join sys.objects o on o.object_id = c.object_id and o.type = 'U'
						  where c.name = 'JobsCreated' and o.name = 'ArraAgreement')
			alter table dbo.ArraAgreement drop column JobsCreated;
			if exists(select 1 from sys.columns c 
						  inner join sys.objects o on o.object_id = c.object_id and o.type = 'U'
						  where c.name = 'JobsRetained' and o.name = 'ArraAgreement')
			alter table dbo.ArraAgreement drop column JobsRetained;

			--kdh3 ARRA  (Creating table for GL close Preview Data)

			IF NOT EXISTS (SELECT * FROM sys.objects 
							WHERE object_id = OBJECT_ID(N'[dbo].[ArraGLClosePreview]') AND type in (N'U'))
			BEGIN
			CREATE TABLE [dbo].[ArraGLClosePreview](
				[FundNumber] [varchar](6) NOT NULL,
				[ExpenseAmount] [money] NULL,
				[RevenueAmount] [money] NULL,
				[FundID] [int] NULL,
				[monthEndingDate] [datetime] NOT NULL,
			 CONSTRAINT [PK_ArraGLClosePreview] PRIMARY KEY CLUSTERED 
			(
				[FundNumber] ASC
			)WITH (PAD_INDEX  = OFF, 
					STATISTICS_NORECOMPUTE  = OFF, 
					IGNORE_DUP_KEY = OFF, 
					ALLOW_ROW_LOCKS  = ON, 
					ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
			) ON [PRIMARY]
			END
			
			IF NOT EXISTS (SELECT * FROM sys.indexes 
							WHERE object_id = OBJECT_ID(N'[dbo].[ArraGLClosePreview]') 
									AND name = N'IX_ArraGLClosePreview_FundID')
			CREATE NONCLUSTERED INDEX [IX_ArraGLClosePreview_FundID] ON [dbo].[ArraGLClosePreview] 
								([FundID] ASC) 
				WITH (PAD_INDEX  = OFF, 
						STATISTICS_NORECOMPUTE  = OFF, 
						SORT_IN_TEMPDB = OFF, 
						IGNORE_DUP_KEY = OFF, 
						DROP_EXISTING = OFF, 
						ONLINE = OFF, 
						ALLOW_ROW_LOCKS  = ON, 
						ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.015

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.016
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231016 )
	BEGIN
		--kdh3 ARRA  (Column updates so not required fields.)
		BEGIN
				alter table arraPrimaryPerformanceLocation 
				alter column Zip varchar(9) null;
				alter table arraPrimaryPerformanceLocation 
				alter column State varchar(2) null;
				alter table arraPrimaryPerformanceLocation 
				alter column City varchar(35) null;
				alter table arraPrimaryPerformanceLocation 
				alter column Country varchar(3) null;
				alter table arraPrimaryPerformanceLocation 
				alter column StreetAddress1 varchar(55) null;
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.016


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 2.3.1.017
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231017 )
	BEGIN
		--kdh3 ARRA  (Column updates so not required fields.)
		BEGIN
				alter table dbo.Stg_ie_Arra_ActivityCode
				alter column code varchar(6)null;

				alter table dbo.Stg_ie_Arra_ProgramSource
				alter column code varchar(7)null;
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.017


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 2.3.1.018  select * from version
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231018 )
	BEGIN
		--kdh3 ARRA  (Column updates so not required fields.)
		BEGIN
				alter table dbo.ArraAgreement
				alter column ProjectStatus varchar(23)null;
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.018


-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.019  select * from version
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231019 )
	BEGIN
		--kdh3 ARRA  (Column updates so not required fields.)
		BEGIN
			IF EXISTS(SELECT * FROM sys.indexes 
					WHERE object_id = OBJECT_ID(N'[dbo].[ArraAgreement]') 
							AND name = N'IX_ArraAgreement_FundID') 
				drop index dbo.ArraAgreement.IX_ArraAgreement_FundID;

			create index IX_ArraAgreement_FundID on dbo.ArraAgreement (FundID);
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.019


-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.020
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231020 )
	BEGIN
		--ys985 Task 512 Remove Chemical Hygiene Plan from the View Full App 
		BEGIN
			UPDATE dbo.RefCodeAttribute
			SET VALUE = 'N'
			WHERE  REFCODE= 'ATCHTYPE_CHEMHYGIENE' AND ATTRIBUTE = 'APP_INCLUDE'

		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.020
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- 2.3.1.021
-------------------------------------------------------------------------------
BEGIN
	IF ( [dbo].[GetNumericDatabaseVersion]() < 231021 )
	BEGIN
		--ys985 Task 512 + Remove SafetyInfo from the View Full App 
		BEGIN
			UPDATE dbo.RefCodeAttribute
			SET VALUE = 'N'
			WHERE  REFCODE= 'ATCHTYPE_SAFETYINFO' AND ATTRIBUTE = 'APP_INCLUDE'
		END

		BEGIN
			set @dbversion = [dbo].[FormatDatabaseVersion]([dbo].[GetNumericDatabaseVersion]() + 1)
            update Version
			set VersionName = @dbversion
			where Description = 'Insight'

		    PRINT 'Insight database version now ' + @dbversion
		END

	END

END -- End 2.3.1.021
-------------------------------------------------------------------------------
